package cn.cool.netty.nio.buffer;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class TestByteBufferString {
    public static void main(String[] args) {
        // 1.字符串转换为 ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(16);
        buffer.put("hello".getBytes());

        // 2. 转换成 Charset
        ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("hello");

        // 3. wrap
        ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());

        // 4. ByteBuffer 转换成字符串
        buffer.flip();
        CharBuffer decode = Charset.defaultCharset().decode(buffer);
        System.out.println(decode.toString());
    }
}
